
var bUseEngine = true;

var bCloseForm = false;

var bSaveInProgress = false;
function CustomFormSubmit()
{

if (bSaveInProgress || (!IsNull(event.returnValue) && !event.returnValue))
{
event.returnValue = false;
return;
}
bCloseForm = (event.Mode == 2);
var isNew = (crmFormSubmit.crmFormSubmitId.value == "");

if (!IsOnline())
{
bUseEngine = false;
}

event.returnValue = true;
if (bUseEngine)
{
if (event.Mode == 1 || event.Mode == 2)
{
event.returnValue = false;
var iStatus = crmForm.BuildXml(false, !isNew);
if (iStatus == 1)
{
if (_bAllowSchedule)
{
var command;


bSaveInProgress = true;
if (isNew)
{
command = new RemoteCommand("ActivitiesWebService", "Book");
}
else
{
command = new RemoteCommand("ActivitiesWebService", "Reschedule");
command.SetParameter("id", crmFormSubmit.crmFormSubmitId.value);
}
command.SetParameter("content", crmFormSubmit.crmFormSubmitXml.value);
command.SetParameter("typeCode", _iActivityTypeCode);
var oResult = command.Execute();


bSaveInProgress = false;
if (oResult.Success)
{

if (oResult.ReturnValue.OperationSuccess)
{
var sNewUrl = location.href;
if (isNew)
{

sNewUrl = sNewUrl.replace(location.search, "");
sNewUrl += "?id=" + oResult.ReturnValue.Id;
}
else
{

sNewUrl = sNewUrl.replace("&mode=reactivate", "");
}
crmForm.detachCloseAlert();




if (!bCloseForm)
{
window.location.href = sNewUrl;
}
RefreshGrid();
}
else
{
var notification = oResult.ReturnValue.Notifications.Notification;
var aNotifications = new Array();
var oNotification;
var args;
if (!isArray(notification))
{
if (notification == null)
{
var oaNotifications = new Array(Notifications.CreateNotification("guid", 1, "strategy", LOCID_GENERIC_SERVER_ERROR));
Notifications.SetNotifications(oaNotifications, "strategy");
}
else
{
args = notification;
oNotification = Notifications.CreateNotification(args.Id, args.Severity, args.Source, args.Text);
aNotifications.push(oNotification);
Notifications.SetNotifications(aNotifications, args.Source);
}
}
else
{
for (var i = 0; i < notification.length; i++)
{
args = notification[i];
oNotification = Notifications.CreateNotification(args.Id, args.Severity, args.Source, args.Text);
aNotifications.push(oNotification);
}
Notifications.SetNotifications(aNotifications, "strategy");
}
ShowNotificationDialog();
}
}
else
{
var oaNotifications = new Array(Notifications.CreateNotification("guid", 1, "strategy", LOCID_GENERIC_SERVER_ERROR));
Notifications.SetNotifications(oaNotifications, "strategy");
ShowNotificationDialog();
}
}
else
{
var oaNotifications = new Array(Notifications.CreateNotification("Cannot_Validate_Activity", 1, "PrivilegeCheck", LOCID_CANNOT_VALIDATE_APPT));
Notifications.SetNotifications(oaNotifications, "PrivilegeCheck");
ShowNotificationDialog();
}
}
else if (iStatus == 2)
{
bCloseForm = false;
}
if (bCloseForm)
{
window.close();
}
}
else
{
event.returnValue = true;
}
}

if (event.returnValue)
{
var sUrl = window.location.href;
sUrl = SetQueryString(sUrl, "loadDialog", "");
sUrl = SetQueryString(sUrl, "autoSearch", "");
sUrl = SetQueryString(sUrl, "_StartTime", "");
crmFormSubmit.action = sUrl;
}
}
function ShowNotificationDialog()
{


bCloseForm = false;
var sResult = "Save";
if (Notifications.HasErrors)
{
var oNotifications = Notifications.GetNotifications();
var sUrl =prependOrgName("/SM/ActivityScheduling/SchedulingNotificationsDialog.aspx");

if (_bEnableSchedulingDialog)
{
sUrl += "?AllowSchedule=true";
}
sResult = openStdDlg(sUrl, oNotifications, 600, 300, true, false);
}

switch (sResult)
{
case "Save":
bUseEngine = false;
event.returnValue = true;
break;
case "Cancel":
break;
case "Schedule":
ScheduleActivity(crmFormSubmit.crmFormSubmitId.value, _iActivityTypeCode);
break;
}
}

function Reactivate(entityName)
{
var token = GetTokenAsQS(prependOrgName('/activities/' + entityName + '/edit.aspx'));
location.search = "?id=" + crmFormSubmit.crmFormSubmitId.value + "&mode=reactivate" + "&" + token;
}
